package com.example.project.controller.statistics;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.example.project.core.controller.BaseController;
import com.example.project.core.domain.AjaxResult;
import com.example.project.service.IndexService;
import com.example.project.service.StatisticsService;
import com.example.project.utils.DateUtils;

/**
 * 统计管理Controller
 * 
 * @author ruoyi
 * @date 2025-10-23
 */
@Controller
@RequestMapping("/statistics")
public class StatisticsController extends BaseController {

	private String prefix = "pm/statistics";

	@Autowired
	private StatisticsService statisticsService;

	@Autowired
	private IndexService indexService;

	@RequiresPermissions("pm:statistics:view")
	@GetMapping("/index")
	public String index(ModelMap mmap) {
		// 获取项目统计
		List<Map<String, Object>> projectStats = statisticsService.getProjectStatistics();
		mmap.put("projectStats", projectStats);

		// 获取问题统计
		List<Map<String, Object>> issueStats = statisticsService.getIssueStatistics();
		mmap.put("issueStats", issueStats);

		Long userId = getUserId();
		indexService.getMain(mmap, userId);
		return prefix + "/statistics_index";
	}

	/**
	 * 获取项目统计图表数据
	 */
	@RequiresPermissions("pm:statistics:view")
	@GetMapping("/projectChartData")
	@ResponseBody
	public AjaxResult getProjectChartData() {
		Map<String, Object> chartData = statisticsService.getProjectChartData();
		return AjaxResult.success(chartData);
	}

	/**
	 * 获取问题统计图表数据
	 */
	@RequiresPermissions("pm:statistics:view")
	@GetMapping("/issueChartData")
	@ResponseBody
	public AjaxResult getIssueChartData() {
		Map<String, Object> chartData = statisticsService.getIssueChartData();
		return AjaxResult.success(chartData);
	}

	/**
	 * 获取项目问题分布统计
	 */
	@GetMapping("/projectIssueDistribution")
	@ResponseBody
	public AjaxResult getProjectIssueDistribution() {
		List<Map<String, Object>> distribution = statisticsService.getProjectIssueDistribution();
		return AjaxResult.success(distribution);
	}

	/**
	 * 获取问题状态统计
	 */
	@RequiresPermissions("pm:statistics:view")
	@GetMapping("/issueStatusStats")
	@ResponseBody
	public AjaxResult getIssueStatusStats() {
		List<Map<String, Object>> statusStats = statisticsService.getIssueStatusStats();
		return AjaxResult.success(statusStats);
	}

	/**
	 * 获取问题优先级统计
	 */
	@RequiresPermissions("pm:statistics:view")
	@GetMapping("/issuePriorityStats")
	@ResponseBody
	public AjaxResult getIssuePriorityStats() {
		List<Map<String, Object>> priorityStats = statisticsService.getIssuePriorityStats();
		return AjaxResult.success(priorityStats);
	}

	@PostMapping("/queryProjectForIssueDetail")
	@ResponseBody
	public AjaxResult queryProjectForIssueDetail() {
		String startDate = DateUtils.getDateBefore(30);
		String endDate = DateUtils.getDate();
		List<Map<String, Object>> distribution = statisticsService.queryProjectForIssueDetail(startDate, endDate, 10);
		return AjaxResult.success(distribution);
	}
}